#define show_menu_hint() display.drawBitmap(48, 48, menu_hint, 32, 16, 1)//菜单
#define show_del_hint() display.drawBitmap(96, 48, del_hint, 32, 16, 1)//删除
#define show_jie_hint() display.drawBitmap(0, 48, jie_hint, 32, 16, 1)//接听
#define show_gua_hint() display.drawBitmap(96, 48, gua_hint, 32, 16, 1)//挂断
#define show_call_hint() display.drawBitmap(0, 48, call_hint, 32, 16, 1)//呼叫
#define show_phone_icon(pos_x) display.drawBitmap(pos_x, 11, phone_icon, 26, 26, 1)
enum STATE{
  MENU_MODE,// 菜单模式
  INPUT_MODE,// 输入号码
  DIALING_MODE,// 来电模式
  TALKING_MODE,// 接听
  CALLING_MODE// 呼叫模式
};

STATE CURR= MENU_MODE;// 记录当前状态
char order[17]="ATD";//指令ATDxxx;\n
void show_menu(){
  display.clearDisplay();//清屏 
  display.drawBitmap(2, 13, icon, 54, 42, 1); //画出LOGO 58x58
  display.drawXBitmap(58, 10, menu1, 70, 13, 1);
  display.drawXBitmap(58, 25, gift, 70, 31, 1);
  display.display();
}
void show_cake(){
  byte FRAME_COUNT= (sizeof(cake_frames) / sizeof(cake_frames[0]));
  for(byte i=0;i<FRAME_COUNT*3;i++){
    display.clearDisplay();
    display.drawBitmap(48, 16, cake_frames[i% FRAME_COUNT], 32, 32, 1);
    display.display();
    delay(42);
  }
  display.clearDisplay();
  display.drawBitmap(4, 12, wish, 120, 43, 1);
  display.display();  
  delay(2500);
  return;
}
STATE show_input(){
  display.clearDisplay();
  show_menu_hint();
  show_call_hint();
  show_del_hint();
  display.display();
  byte phone_num=0;// 数字个数，最大限制为11
  order[0]='A',order[1]='T',order[2]='D';
  memset(order+3,0,14); 

  while(1){
    byte key = keypad.getKey();// Read the key
    if (key){
      if(key>0&&key<13){// 输入的文字
        if(phone_num>10)continue;//输入超了11位

        Serial.print("Key Pressed : ");
        Serial.println(int(key));
        
        display.drawBitmap(phone_num*11+ 3, 15, num[key], 11, 18, 1); //画出字符对应点阵数据
        if(key==11)order[phone_num+3]='0';
        else order[3+phone_num]=char(key+'0');
        phone_num++;
        
      }else if(key==13){// 呼叫
        if((order[3]=='0')&&(order[4]=='2')&&(order[5]=='0')&&(order[6]=='2')&&(order[7]=='\0')){
          show_cake();
          return MENU_MODE;
        }

        order[(phone_num++)+3]=';';
        order[phone_num+3]='\n';

        lock=true;
        DebugSerial.write(order);
        lock=false;

        Serial.print("order_phone_num:");
        Serial.println(phone_num);
        Serial.println(order);

        return CALLING_MODE;
       }else if(key==14){//菜单
        memset(order+3,0,14);
        return MENU_MODE;
       }else{//删除
        if(phone_num<1){continue;}//没有输入就按删除了
        --phone_num;
        display.fillRect(phone_num*11+ 3, 15, 11 , 18, BLACK);// 清空该位置数字
        order[phone_num+3]='\0';//只有2个数字时，order[4]  
      }
      display.display();
    }
  }

}
void draw_num(char phone_num[]){
  for(byte i=0;i<11&&(phone_num[i+3]!=';');i++){// 显示拨号的号码
    display.drawBitmap(i*11+ 3, 15, num[phone_num[i+3]-'0'], 11, 18, 1); //画出字符对应点阵数据
  }
}


void show_calling(char phone_num[]){//呼叫界面
  CURR= CALLING_MODE;
  display.clearDisplay();
  show_gua_hint();
  draw_num(phone_num);
  display.display();

  unsigned long startTime = millis();
  while(CURR!=MENU_MODE){
    if(millis()-startTime>60000)break;//超时自动挂断
    byte key= keypad.getKey();
    if(key==14||key==15){
      lock=true;
      DebugSerial.write("ATH\n");
      lock=false;
      break;
    }
  }
  CURR=MENU_MODE;
  return;
}
void show_dialing(){
  display.clearDisplay();
  show_gua_hint();
  show_jie_hint();
  display.drawBitmap(19, 17, calling, 93, 14, 1);
  display.display();
}
void show_talking(){
  display.clearDisplay();
  show_gua_hint();
  display.drawBitmap(19, 17, calling, 93, 14, 1);
  display.display();
}
